package com.esd.device.controller;

import com.esd.common.pojo.R;
import com.esd.device.entity.ChannelType;
import com.esd.device.service.ChannelTypeService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("channelType")
public class ChannelTypeController {

    @Autowired
    private ChannelTypeService channelTypeService ;

    @GetMapping("listAll")
    public R listAll(){
        return R.ok(channelTypeService.list()) ;
    }

    @PostMapping("addChannelType")
    public R addChannelType(@RequestBody ChannelType channelType){
        return channelTypeService.addChannelType(channelType);
    }

    @GetMapping("getById/{channelTypeId}")
    public R getById(@PathVariable Long channelTypeId){
        return R.ok(channelTypeService.getById(channelTypeId));
    }

    @PutMapping("updateChannelType")
    public R updateChannelType(@RequestBody ChannelType channelType){
        return channelTypeService.updateChannelType(channelType);
    }

    @DeleteMapping("deleteChannelType/{channelTypeId}")
    public R deleteChannelType(@PathVariable Long channelTypeId){
        return channelTypeService.deleteChannelType(channelTypeId);
    }

    @Value("${esd.channelTypes}")
    private String channelTypesJson ;

    @GetMapping("getChannelTypes")
    public List<Map<String , Object>> getChannelTypes() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        List<Map<String , Object>> list = mapper.readValue(
                new File(channelTypesJson),
                mapper.getTypeFactory().constructCollectionType(List.class, Map.class));
        return list;
    }

}
